package com.tdkj.service.financial.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.model.exchangerate.BeanExchangeRate;
import com.tdkj.model.financial.BeanExchangeRecord;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.ServiceException;
import com.tdkj.service.exchangerate.IBeanExchangeRateService;
import com.tdkj.service.financial.IBeanExchangeRecordService;
import com.tdkj.service.financial.IBeanFimamcialAmountChangeRecordService;

@Component
public class BeanExchangeRecordServiceImpl extends BaseService implements
		IBeanExchangeRecordService {

	private IBeanExchangeRateService exchangeRateService;
	
	private IBeanFimamcialAmountChangeRecordService fimamcialAmountChangeRecordService;
	
	@Override
	public void create(Long userId, Integer integral) throws ServiceException {
		BeanExchangeRate rate = exchangeRateService.getExchangeRate();
		BeanUser user = this.getBaseDao().findById(userId, BeanUser.class);
		
		Integer revenue = (int)(integral/rate.getIntegral());
		
		BeanExchangeRecord bean = new BeanExchangeRecord();
		bean.setPay(integral);
		bean.setPayUnit("积分");
		bean.setRevenue(revenue);
		bean.setPayUnit("银币");
		bean.setDateTime(new Timestamp(System.currentTimeMillis()));
		bean.setUser(user);
		
		this.getBaseDao().save(bean);
		fimamcialAmountChangeRecordService.createOut(userId, integral, "积分", bean.getId(), BeanExchangeRecord.class, "积分兑换");
		fimamcialAmountChangeRecordService.createIn(userId, revenue, "银币", bean.getId(), BeanExchangeRecord.class, "积分兑换");
	}

	@Resource
	public void setExchangeRateService(IBeanExchangeRateService exchangeRateService) {
		this.exchangeRateService = exchangeRateService;
	}

	@Resource
	public void setFimamcialAmountChangeRecordService(
			IBeanFimamcialAmountChangeRecordService fimamcialAmountChangeRecordService) {
		this.fimamcialAmountChangeRecordService = fimamcialAmountChangeRecordService;
	}

	
}
